import { getDefaultFields } from '../utils';
import { ORDER_TYPE } from '../common/constants';

module.exports = app => {
    const DataTypes = app.Sequelize;

    const Model = app.model.define('order', {
        id: {
            type: DataTypes.INTEGER(8).UNSIGNED,
            allowNull: false,
            primaryKey: true,
            autoIncrement: true
        },
        user_id: {
            type: DataTypes.STRING(50),
            allowNull: false,
            comment: '用户ID'
        },
        order_id: {
            type: DataTypes.STRING(100),
            allowNull: false,
            defaultValue: '0',
            comment: '平台的商品ID',
        },
        table_cost: {
            type: DataTypes.INTEGER(11),
            allowNull: true,
            defaultValue: 0,
            comment: '堂食订单餐具费'
        },
        pack_cost: {
            type: DataTypes.INTEGER(11),
            allowNull: true,
            defaultValue: 0,
            comment: '外卖订单打包费'
        },
        all_price: {
            type: DataTypes.INTEGER(11),
            allowNull: true,
            defaultValue: 0,
            comment: '购买商品的总价'
        },
        create_time: {
            type: DataTypes.STRING(50),
            allowNull: true,
            defaultValue: null,
            comment: '订单创建时间'
        },
        status: {
            type: DataTypes.STRING(10),
            allowNull: false,
            defaultValue: 'WAIT_PAY',
            enum: ['WAIT_PAY', 'PAY', 'DONE', 'CANCEL', 'REFUND', 'READY', 'SENDING', 'RATE'],
            comment: 'WAIT_PAY:未付款, PAY:已支付, CANCEL: 已取消, REFUND:已退款  DONE:已完成 READY:配货中 SENDING: 配送中 RATE: 已评价'
        },
        mch_id: {
            type: DataTypes.STRING(32),
            allowNull: true,
            defaultValue: null,
            comment: '商户号'
        },
        trade_type: {
            type: DataTypes.STRING(16),
            allowNull: true,
            defaultValue: null,
            comment: '交易类型'
        },
        // 付款银行
        bank_type: {
            type: DataTypes.STRING(32),
            allowNull: true,
            defaultValue: null
        },
        total_fee: {
            type: DataTypes.INTEGER(11),
            allowNull: true,
            defaultValue: null,
            comment: '订单实际支付金额'
        },
        cash_fee: {
            type: DataTypes.STRING(16),
            allowNull: true,
            defaultValue: null,
            comment: '现金支付金额'
        },
        transaction_id: {
            type: DataTypes.STRING(32),
            allowNull: true,
            defaultValue: null,
            comment: '微信支付订单号'
        },
        pay_status: {
            type: DataTypes.STRING(1),
            allowNull: false,
            defaultValue: 0,
            enum: [0, 1],
            comment: '0:未支付 1: 支付'
        },
        pay_time: {
            type: DataTypes.STRING(32),
            allowNull: true,
            defaultValue: null,
            comment: '支付时间'
        },
        shop_id: {
            type: DataTypes.INTEGER(11),
            allowNull: true,
            defaultValue: null,
            comment: '商家ID，预留SASS功能'
        },
        note: {
            type: DataTypes.STRING(255),
            allowNull: true,
            defaultValue: '',
            comment: '订单备注'
        },
        waybill_id: {
            type: DataTypes.STRING(100),
            allowNull: true,
            defaultValue: null,
            comment: '快递号'
        },
        shipping_status: {
            type: DataTypes.INTEGER(1),
            allowNull: false,
            defaultValue: -1,
            comment: '物流状态，-1 未开始配送 0 开始配送 1 完成配送 2 取消配送'
        },
        shipping_fee: {
            type: DataTypes.DECIMAL(10, 4),
            allowNull: false,
            defaultValue: '0.00',
        },
        shipping_time: {
            type: DataTypes.INTEGER(11),
            allowNull: false,
            defaultValue: '0',
            comment: '配送完成时间',
        },
        out_refund_no: {
            type: DataTypes.STRING(30),
            allowNull: true,
            defaultValue: null,
            comment: '退款单号'
        },
        people_number: {
            type: DataTypes.INTEGER(11),
            allowNull: true,
            defaultValue: null,
            comment: '就餐人数'
        },
        table_number: {
            type: DataTypes.STRING(30),
            allowNull: true,
            defaultValue: null,
            comment: '桌位号'
        },
        meal_number: {
            type: DataTypes.INTEGER(11),
            allowNull: true,
            defaultValue: 0,
            comment: '取餐号'
        },
        order_type: {
            type: DataTypes.STRING(12),
            allowNull: false,
            enum: ORDER_TYPE,
            comment: '订单类型, INSTORE: 堂食订单、TAKEOUT: 外卖订单、PICKUP：自提'
        },
        statusText: {
            type: DataTypes.VIRTUAL
        },
        ...getDefaultFields()
    }, {
        tableName: 'order',
        timestamps: false,
        getterMethods: {
            statusText() {
                switch (this['status']) {
                    case 'WAIT_PAY':
                        return '未付款';
                    case 'CANCEL':
                        return '已取消';
                    case 'PAY':
                        return '已付款';
                    case 'READY':
                        return '配货中';
                    case 'SENDING':
                        return '配送中';
                    case 'DONE':
                        return '待评价';
                    case 'RATE':
                        return '已完成';
                    default:
                        return '未付款';
                }
            }
        }
    });

    Model.sync({ alter: true });

    Model.associate = function () {
        // 用户信息，通过收货地址
        app.model.Order.hasOne(app.model.OrderAddress, {
            foreignKey: 'order_id',
            constraints: false,
            sourceKey: 'order_id'
        });
        app.model.Order.hasMany(app.model.OrderGoods, {
            foreignKey: 'order_id',
            constraints: false,
            sourceKey: 'order_id'
        });
        app.model.Order.belongsTo(app.model.MUser, {
            foreignKey: 'user_id',
            constraints: false,
            targetKey: 'user_id'
        });
        app.model.Order.hasOne(app.model.OrderExpress, {
            foreignKey: 'order_id',
            constraints: false,
            sourceKey: 'order_id'
        });

    }

    return Model;
};
